<?php
namespace app\admin\controller;
use app\common\model;
use app\common\service\BaiduApiService;
use app\common\service\MeetingService;
use think\Exception;

class Meeting extends Base
{
    public $meetingModel;
    public function __construct() {
        parent::__construct();
        $this->meetingModel = new model\Meeting();
    }

    public function index () {
        $where = 1;
        $page = max($_GET['page'],1);
        $size = config('page_size');
        $datalist = $this->meetingModel->getPage($where,$page,$size,'create_time desc');
        $pager = $this->meetingModel->where($where)->paginate($size)->render();
        $this->assign('pager', $pager);
        $this->assign('datalist',$datalist);
        return view();
    }

    public function add () {

        $input = input();
        if($input['id']) {
            $res = $this->meetingModel->getOne(['id'=>$input['id']]);
            $where = ['id'=>$input['id']];
        }
        if(IS_AJAX) {
            $data = $input;
            if($res) {
                $this->meetingModel->where($where)->update($data);
            }else{
                $insert = $data;
                $insert['create_time'] = getDateTime();
                $this->meetingModel->insert($insert);
            }
            return ajaxReturn(0,'保存成功');
        }
        $this->assign('data',$res);
        $this->view->engine->layout('common/login-layout'); 
        return view();
    }

    public function del () {
        $input = input();
        $res = $this->meetingModel->getOne(['id'=>$input['id']]);
        if(!$res) {
            return ajaxReturn(1,'数据不存在','');
        }
        $this->meetingModel->where(['id'=>$input['id']])->delete();
        return ajaxReturn(0,'已删除','');
    }

    //创建会议
    public function createMeeting(){
        try{
            $add['meeting_name']=input('post.meeting_name');
            $add['start_time']=input('post.start_time');
            if(empty($add['start_time'])){
                $add['start_time']=date('Y-m-d H:i:s');
            }
            $add['end_time']=input('post.end_time');
            if(empty($add['end_time'])){
                $add['end_time']=date("Y-m-d", strtotime("+1 year", strtotime($add['start_time'])));
            }
            $add['gid']=BaiduMeeting::$gid;
            $add['desc']=input('post.desc');
            $add['headimage']=input('post.headimage');
            if(isset($_POST['id']) && !empty($_POST['id'])){
                $id=input('post.id');
                $re=MeetingService::S()->saveMeeting($id,$add);
                if($re===false){
                    $id=false;
                }
            }else{
                $id=MeetingService::S()->addMeeting($add);
            }
            if($id){//写入成功像百度创建门店
                $headimage=base64EncodeImage($add['headimage']);
                /*$re=BaiduApiService::S()->addProject(
                    $id,
                    $add['gid'],
                    $add['meeting_name'],
                    strtotime($add['start_time']),
                    $add['desc'],strtotime($add['end_time']),
                    $headimage);*/
                $this->ajax(200,'success');
            }
        }catch (Exception $ex){
            echo '接口异常:'.$ex->getMessage();
        }
    }


}
